<?php

namespace addons\cms\model;

use app\common\library\Auth;
use app\common\library\Email;
use think\Exception;
use think\Model;
use think\Validate;

/**
 * 评论模型
 */
class Msg Extends Model
{

    protected $name = "cms_msg";
    //自定义初始化
    protected static function init()
    {

    }


    /**
     * 发表评论
     * @param array $params
     * @return bool
     * @throws Exception
     */
    public static function postMsg($type=1,$archives_id=0,$receive_id=0,$content='')
    {
        $archives_title = '';
        if($archives_id){
            $archives = Archives::get($archives_id);
            $receive_id = $archives['user_id'];
            $archives_title = $archives['title'];
        }

        $auth = Auth::instance();
        $request = \think\Request::instance();

        $params = [
            'user_id'     => $auth->id ? $auth->id : 0,
            'receive_id' => $receive_id,
            'archives_id' => $archives_id,
            'type'       => $type,
            'content'      => $content,
            'archives_title' => $archives_title,
            'createtime'       => time(),
            'updatetime'       => time()
        ];
        (new static())->allowField(true)->save($params);

        return true;
    }



    /**
     * 关联会员模型
     */
    public function user()
    {
        return $this->belongsTo("app\common\model\User")->field('id,nickname,avatar,email')->setEagerlyType(1);
    }

    /**
     * 关联文章模型
     */
    public function archives()
    {
        return $this->belongsTo("addons\cms\model\Archives")->field('id,title,image,diyname,description,collection,model_id,money,channel_id,likes,dislikes,tags,createtime')->setEagerlyType(1);
    }

}
